#include "StdAfx.h"
#include "Frame.hxx"
#include "TrackerView.hxx"
#include "TrackerModel.hxx"
#include "ChannelView.hxx"
#include "SettingView.hxx"
#include "ChannelFourBoxView.hxx"

TrackerView::TrackerView(void)
{
	_rectWidth = 0;
	_rectHeight = 0;

	for( int i = 0; i < CHANNEL_COUNT; i ++ )
		_channelViews[ i ] = 0;

	_channelFourView = 0;

	_settingView = 0;
	
	_isSettingViewVisible = false;

	_model = 0;
}


TrackerView::~TrackerView(void)
{
	for( int i = 0; i < CHANNEL_COUNT; i++ )
	{
		if( _channelViews[ i ] )
		{
			delete _channelViews[ i ];
			_channelViews[ i ] = 0;
		}
	}

////////////////////////Tian/////////////////////////////////////////////
	if (_channelFourView)
	{
		delete _channelFourView;
		_channelFourView = 0;
	}
//////////////////////////////////////////////////////////////////////////

	if( _settingView )
	{
		delete _settingView;
		_settingView = 0;
	}
}

void TrackerView::initialize()
{
	for( int i = 0; i < CHANNEL_COUNT; i ++ )
	{
		_channelViews[ i ] = new ChannelView( this );
		_channelViews[ i ]->setChannelIndex( i );
	}

	/////////////////////////Tian///////////////////////////////////////

	_channelFourView = new ChannelFourBoxView(this);

	for(int i=0;i<CHANNEL_COUNT; i++)
	{
		if( _channelFourView )
			_channelFourView->setView(_channelViews[i],i);
	}
	
	////////////////////////////////////////////////////////////////////

	_settingView = new SettingView( this );
	_settingView->setChannelIndex( 0 );
}

void TrackerView::setSubViewModels()
{
	for( int i = 0; i < CHANNEL_COUNT; i ++ )
	{
		if( _channelViews[ i ] )
			_channelViews[ i ]->setModel( _model->getSubModel( i ) );
	}
	/////////////////////////Tian///////////////////////////////////////

	for(int i=0;i<CHANNEL_COUNT; i++)
	{
		if( _channelFourView )
			 _channelFourView->setModel( _model->getSubModel(i),i);
	}
	////////////////////////////////////////////////////////////////////

	_settingView->setModel( _model->getSubModel( 0 ) );

}

void TrackerView::reshape( int w, int h )
{
	_rectWidth = w;
	_rectHeight = h;

	for( int i = 0; i < CHANNEL_COUNT; i ++ )
	{
		_channelViews[ i ]->reshape( w, h );
	}

	//////////////////////////Tian////////////////////////////////////////
	
	_channelFourView ->reshape(w,h);

	//////////////////////////////////////////////////////////////////////
}

void TrackerView::render()
{
	//////////////////////////Tian////////////////////////////////////////

	_channelFourView ->render();

	//////////////////////////////////////////////////////////////////////

	for( int i = 0; i < CHANNEL_COUNT; i ++ )
		_channelViews[ i ]->render();
}

//void TrackerView::initializeSubViews()
//{
//	for( int i = 0; i < CHANNEL_COUNT; i ++ )
//		_channelViews[ i ]->initialize();
//}

void TrackerView::changeSettingViewChannel( int chnIndex )
{
	if( chnIndex == _settingView->getChannelIndex() )
		return;

	ChannelModel* model = _model->getSubModel( chnIndex );
	_settingView->setModel( model );
	_settingView->setChannelIndex( chnIndex );
}
